package com.dk.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.PermissionsMapper;
import com.dk.pojo.Permissions;
import com.dk.pojo.PermissionsExample;
import com.dk.pojo.UUser;
import com.dk.service.PermissionsServiceI;
import constant.RedisNewsConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionsServiceImpl implements PermissionsServiceI {
    @Autowired
    private PermissionsMapper permissionsMapper;


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Map<String, Object>> getTSource(UUser uUser) {

        //五种获取方式
   //     redisTemplate.boundValueOps(); string
   //     redisTemplate.boundHashOps(); hash
   //     redisTemplate.boundListOps(); list
   //     redisTemplate.boundSetOps(); set
  //      redisTemplate.boundZSetOps(); zset

 /*      //查询数据库之前，先查询redis里面是否有(value,所以转为String)
        String per = (String)redisTemplate.boundValueOps("permissions:"+uUser.getUid()).get();
        //如果数据库没有或者不是空字符串，就查返回redis中的数据
        if(per != null && !"".equals(per)){
            //查询出来的String转为List类型
            List<Permissions> permissions = JSONObject.parseArray(per, Permissions.class);
            List<Map<String, Object>> tree = getTree(permissions,0);
            return  tree;
        }else {
            //权限表查询，员工权限条件查询
            List<Permissions> permissions = permissionsMapper.selectById(uUser.getUid());

            String s = JSONObject.toJSONString(permissions);

            //查询完数据库，放到Redis里面
            redisTemplate.boundValueOps("permissions:"+uUser.getUid()).set(s);
           //调用递归接口返回递归遍历拼接完成的数据
            List<Map<String, Object>> tree = getTree(permissions,0);
            return tree;
        }*/

        String Permissionss = "permissionss:"+uUser.getUid();

            //先判断redis是否有key,如果没有这个key的话
        if(!redisTemplate.hasKey(Permissionss)){
            //权限表查询，员工权限条件查询
            List<Permissions> permissions = permissionsMapper.selectById(uUser.getUid());
            for (Permissions list :permissions){
                redisTemplate.boundHashOps(Permissionss).put(list.getFeId(),JSON.toJSONString(list));
            }
            List<Map<String, Object>> tree = getTree(permissions,0);
                return tree;
        }else {
            List<Object> values = redisTemplate.boundHashOps(Permissionss).values();
            List<Permissions> permissions = new ArrayList<>();
            for (Object s : values){
                permissions.add(JSON.parseObject((String)s,Permissions.class));
            }
            List<Map<String, Object>> tree = getTree(permissions,0);
            return tree;
        }
    }


    public List<Map<String,Object>> getTree(List<Permissions> permissions,Integer pid){
        List<Map<String, Object>> listTree = new ArrayList<>();
        for (int i = 0; i<permissions.size(); i++){
            Permissions perm = permissions.get(i);
            Map<String,Object>  map =  null;

            if(perm.getFePid().equals(pid)){
                map = new HashMap<>();
                map.put("id",perm.getFeId());
                map.put("text",perm.getFeName());
                map.put("url",perm.getFeUrl());
                map.put("icon",perm.getFeIcon());
                map.put("nodes",getTree(permissions,perm.getFeId()));
            }
            if(map != null){
                List<Map<String, Object>> li = (List<Map<String, Object>>) map.get("nodes");
                if(li.size() == 0){
                    map.remove("nodes");
                }
                listTree.add(map);
            }
        }
        return listTree;
    }


    //ZTree
    @Override
    public List<Map<String, Object>> getzTree() {
        List<Permissions> permissions = permissionsMapper.selectByExample(new PermissionsExample());
        List<Map<String, Object>> tree = getzTree(permissions,0);
        return tree;
    }

    public List<Map<String,Object>> getzTree(List<Permissions> permissions,Integer pid){
        List<Map<String, Object>> listTree = new ArrayList<>();
        for (int i = 0; i<permissions.size(); i++){

            Permissions perm = permissions.get(i);

            Map<String,Object>  map =  null;
            if(perm.getFePid().equals(pid)){
                map = new HashMap<>();
                map.put("id",perm.getFeId());
                map.put("name",perm.getFeName());
                //防止页面点击跳转
              /*  map.put("url",perm.getFeUrl());*/
                map.put("icon",perm.getFeIcon());
                map.put("children",getzTree(permissions,perm.getFeId()));
            }
            if(map != null){
                List<Map<String, Object>> li = (List<Map<String, Object>>) map.get("children");
                if(li.size() == 0){
                    map.remove("children");
                }
                listTree.add(map);
            }
        }
        return listTree;
    }

    @Override
    public void addInfos(Permissions permissions,UUser uUser) {
        String Permissionss = "permissionss:"+uUser.getUid();
        permissionsMapper.insertSelective(permissions);
        redisTemplate.delete(Permissionss);

    }

    @Override
    public Permissions getInfo(Integer feId,UUser uUser) {
        String Permissionss = "permissionss:"+uUser.getUid();
        String permissions = (String) redisTemplate.boundHashOps(Permissionss).get(feId);
        Permissions permissions1 = JSONObject.parseObject(permissions,Permissions.class);
        return permissions1;
    }

    @Override
    public void updateInfo(Permissions permissions,UUser uUser) {
        String Permissionss = "permissionss:"+uUser.getUid();
        permissionsMapper.updateByPrimaryKeySelective(permissions);
        redisTemplate.delete(Permissionss);
    }

    @Override
    public void deleteInfo(Integer feId,UUser uUser) {
        String Permissionss = "permissionss:"+uUser.getUid();
        permissionsMapper.deleteByPrimaryKey(feId);
        redisTemplate.boundHashOps(Permissionss).delete(feId);
    }


    @Override
    public List<Map<String, Object>> getzCheckedTree(Integer rid) {
        List<Permissions> permissions = permissionsMapper.selectByExample(new PermissionsExample());
        List<Integer> Rrid = permissionsMapper.getzCheckedZTree(rid);
        List<Map<String, Object>> tree = getzCheckedTree(permissions,0,Rrid);
        return tree;
    }

    public List<Map<String,Object>> getzCheckedTree(List<Permissions> permissions,Integer pid,List<Integer> rid) {
        List<Map<String, Object>> listTree = new ArrayList<>();

        for (int i = 0; i < permissions.size(); i++) {

            Permissions perm = permissions.get(i);

            Map<String, Object> map = null;
            if (perm.getFePid().equals(pid)) {
                map = new HashMap<>();
                map.put("id", perm.getFeId());
                map.put("name", perm.getFeName());
                map.put("url", perm.getFeUrl());
                map.put("icon", perm.getFeIcon());
                if(rid.contains(perm.getFeId())){
                    map.put("checked",true);
                }
                map.put("children", getzCheckedTree(permissions, perm.getFeId(),rid));
            }
            if (map != null) {
                List<Map<String, Object>> li = (List<Map<String, Object>>) map.get("children");
                if (li.size() == 0) {
                    map.remove("children");
                }
                listTree.add(map);
            }
        }

        return listTree;
    }
}
